<?php

/**
 * @copyright : ienter asia Technologies
 * @author :  Hunglv
 * @version    1.0
 * @Description : Class Module Leave registration
 */
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Mod_leaveregistration extends MYAD_Controller {
    /*
     * @author :        HungLV
     * @created:        
     * @Description:    Load Model Leave registration
     * @Input :         Null
     * @Output :        Null
     */

    function __construct() {
        parent::__construct();
        $this->load->model('Mmod_leaveregistration');
        $this->load->model('mod_leaveTypes/Mmod_leavetypes');
        $this->load->model('mod_employee/Mmod_employee');
        $this->load->model('mod_tcmanipulation/Mmod_tcmanipulation', 'tcm');
    }

    /*
     * @author :        HungLV
     * @created:        
     * @Description:    get all data leave registration for view
     * @Input :         Null
     * @Output :        Return array data for view
     */

    function index($page = 0) {
        $data = $this->Mmod_leaveregistration->getAllData($page);
        $this->load->view('layout', $data);
    }

    /*
     * @author :        HungLV
     * @created:        
     * @Description:    Add leave registration
     * @Input :         $EmployeeId=""
     * @Output :        null
     */

    function addleaveregistration() {
        $temp = array();
        $data1 = array();
        //del session
        if($this->input->post('hidDel')=='true'){
            $this->Mmod_leaveregistration->unsetDataSession();            
        }
        $employeeId = $this->input->post('hidAddEmployeeId')?$this->input->post('hidAddEmployeeId'):$this->input->post('hidEmployeeId');
        $temp['data'] = $this->Mmod_leaveregistration->getAllLeaveType();
        if ($this->input->post('btnSubmit')) {
            $validate = $this->Mmod_leaveregistration->validateInput();
            $temp['cboLeavetype'] = $this->input->post('cboLeavetype');
            if ($validate) {              
                $data1 = $this->Mmod_leaveregistration->newLeaveregistrationData();
            }
        }
        if (isset($employeeId) && is_numeric($employeeId)) {
            $temp['Employee'] = $this->Mmod_leaveregistration->loadEmployeeId($employeeId);
        }
        $infoleaveregistration = $this->Mmod_leaveregistration->getInfoLeaveregistration();
        $data = array_merge($temp, $data1, $infoleaveregistration);
        $this->load->view('layout', $data);
    }
    /*
     * @author :        HungLV
     * @created:        
     * @Description:    Get all employee for chose employee leave registration
     * @Input :         $employee id
     * @Output :        array data for view
     */

    function searchEmployee($page = 0) {
        $data = $this->Mmod_leaveregistration->allEmployee($page);
        $this->load->view('layout', $data);
    }

    /*
     * @author :        HungLV
     * @created:        
     * @Description:    Edit leave registration by id
     * @Input :         $id registration
     * @Output :        null
     */

    function editleaveregistration() {
        $temp = array();
        $data1 = array();
        $leaveType= array();
        if($this->input->post('hidDel')=='true'){
            $this->Mmod_leaveregistration->unsetDataSession();             
        }
        $employeeID = $this->input->post('hidEmployeeId');
        $RegistrationId = $this->input->post('hidLeaveId');
        if (isset($employeeID) && $this->Mmod_leaveregistration->checkEmployeeId($employeeID)) {
            $temp['Employee'] = $this->Mmod_leaveregistration->loadEmployeeId($employeeID);
        }
        else{
            redirect('/leaveregistration');
        }
        if (is_numeric($RegistrationId)) {
            if ($this->Mmod_leaveregistration->checkIdLeaveRegistration($RegistrationId)) {
                $temp['leavetypes'] = $this->Mmod_leavetypes->listAll();
                if ($this->input->post('btnSubmit')) {
                    if ($this->Mmod_leaveregistration->checkIdLeaveRegistration($RegistrationId)) {
                        $validate = $this->Mmod_leaveregistration->validateInput();
                        $temp['cboLeavetype'] = $this->input->post('cboLeavetype');
                        if ($validate) {
                            $data1 = $this->Mmod_leaveregistration->updateLeaveregistrationData();
                        } else {
                            $temp['data'] = $this->Mmod_leaveregistration->getPostLeaveRegistration();
                        }
                    } else {
                        redirect('/leaveregistration');
                    }
                    $temp['chkDelFlag'] = $this->input->post('chkDelFlag');
                } else {
                    $leaveType= $this->Mmod_leaveregistration->getLeaveRegistrationByID($RegistrationId);
                    $temp['data']  = $leaveType;
                    $temp['chkDelFlag'] = $leaveType['Del_Flag'];
                }
                $infoleaveregistration = $this->Mmod_leaveregistration->getInfoEditLeaveregistration($leaveType,$employeeID);
                $data = array_merge($temp, $data1, $infoleaveregistration);
                $this->load->view('layout', $data);
            } else {
                redirect('/leaveregistration');
            }
        } else {
            redirect('/leaveregistration');
        }
    }

    /*
     * @author :        HungLV
     * @created:        
     * @Description:    Validate empty select box
     * @Input :         $str value
     * @Output :        Return true or false
     */

    function validate_select($str) {
        if ($str == 0) {
            $this->form_validation->set_message('validate_select', $this->lang->line('LR_required'));
            return FALSE;
        }
        return TRUE;
    }

    /*
     * @author :        HungLV
     * @created:        
     * @Description:    Validate start time and end time
     * @Input :         $str value
     * @Output :        Return true or false
     */

    function validate_timeend($endtime = "", $starttime = "") {
        $starDate = $this->input->post('txtStartDate');
        $endDate = $this->input->post('txtEndDate');
        $endTime = strtotime($endDate . ' ' . $endtime . ':00');
        $startTime = strtotime($starDate . ' ' . $this->input->post($starttime) . ':00');
        if ($endTime <= $startTime) {
            $this->form_validation->set_message('validate_timeend', '%s ' . $this->lang->line('LR_validate_date_lessthan'));
            return FALSE;
        } else {
            $shipInfo = $this->tcm->listShiftInfo();
            if (!$shipInfo || (($endTime - $startTime) < $shipInfo['RoundedTimeBlock'] * 60)) {
                $this->form_validation->set_message('validate_timeend', $this->lang->line('LR_validate_date_notfound_shipinfo'));
                return FALSE;
            }
            return TRUE;
        }
    }

}

?>
